import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
 * @author robert.kraneis
 */
public class Main {
	
	public static JFrame jFrame;
	public static Robot robot;
	public static JTextField ipField;
	public static JTextField nameField;
	public static JTextField colorField;
	
	public static boolean connected = false;
	public static boolean moving = false;
	public static boolean placedMine = false;
	public static boolean firing = false;
	
	public static void connect() {
		
		connected = true;
		int color;
		
		switch (colorField.getText().toLowerCase()) {
		case "green":
			color = 0;
			break;
		case "red":
			color = 1;
			break;
		case "yellow":
			color = 2;
			break;
		case "blue":
			color = 3;
			break;
		case "purple":
			color = 4;
			break;
		case "lightblue":
			color = 5;
			break;
		case "orange":
			color = 6;
			break;
		default:
			color = 7;
			break;
		}
		
		robot = new Robot(
				ipField.getText(),
				nameField.getText(),
				color
				);
		
		jFrame.dispose();
		
		jFrame = new JFrame("RW");
		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jFrame.pack();
		
		jFrame.setVisible(true);
		jFrame.setResizable(false);
		
		jFrame.addKeyListener(new KeyAdapter() {
			
			@Override
			public void keyPressed(KeyEvent e) {
				switch (e.getKeyChar()) {
				case 'w':
					if (!moving) {
						robot.move(2000);
						moving = true;
					}
					break;
				case 'a':
					if (!moving) {
						robot.turn(-200);
						moving = true;
					}
					break;
				case 'd':
					if (!moving) {
						robot.turn(200);
						moving = true;
					}
					break;
				case 's':
					if (!moving) {
						robot.move(-2000);
						moving = true;
					}
					break;
				case ' ':
					if (!firing) {
						robot.fire();
						firing = true;
					}
					break;
				case 'e':
					if (!placedMine) {
						robot.placeMine();
						placedMine = true;
					}
					break;
				case 'r':
					robot.requestRange();
					robot.requestGroundData();
					break;
				case 't':
					System.out.println("DONE: " + robot.isDone());
					System.out.println("Range: " + robot.getDistance());
					System.out.println("Angle: " + robot.getAngle());
					System.out.println("GroundData: " + robot.getGroundSensorData());
					break;
				}
			}
			
			@Override
			public void keyReleased(KeyEvent e) {
				switch (e.getKeyChar()) {
				case 'w':
					if (moving) {
						robot.idle();
						moving = false;
					}
					break;
				case 'a':
					if (moving) {
						robot.idle();
						moving = false;
					}
					break;
				case 'd':
					if (moving) {
						robot.idle();
						moving = false;
					}
					break;
				case 's':
					if (moving) {
						robot.idle();
						moving = false;
					}
					break;
				case ' ':
					firing = false;
					break;
				case 'e':
					placedMine = false;
					break;
				}
			}
		});
		
		
	}

	public static void main(String[] args) {
		createWindow();
	}
	
	public static void createWindow() {
		jFrame = new JFrame("RobotWorld Debugging Client");
		jFrame.setSize(400, 300);
		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		FlowLayout layout = new FlowLayout(FlowLayout.CENTER, 10, 10);
		
		jFrame.getContentPane().setLayout(layout);

		jFrame.add(new JLabel("IP:"));
		ipField = new JTextField(10);
		ipField.setText("127.0.0.1");
		jFrame.add(ipField);
		
		jFrame.add(new JLabel("Name:"));
		nameField = new JTextField(10);
		nameField.setText("Fred");
		jFrame.add(nameField);
		
		jFrame.add(new JLabel("Color:"));
		colorField = new JTextField(10);
		colorField.setText("yellow");
		jFrame.add(colorField);
		
		JButton connectButton = new JButton("Connect");
		jFrame.add(connectButton);
		
		connectButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if (!connected) {
					connect();
				}
			}
		});
		
		jFrame.pack();
		
		jFrame.setLocationRelativeTo(null); 
		jFrame.setVisible(true);
	}

}
